﻿using System;
using System.Collections.Generic;
using TracNghiemChonTruong.Class;

namespace TracNghiemChonTruong
{
    public partial class SearchPage : System.Web.UI.Page
    {
        const string Query = "SELECT TruongHoc.MaTruong, TruongHoc.TenTruong, TenNganh, KhoiThi, MaHe, Diem FROM TruongHoc LEFT JOIN NganhTruong ON TruongHoc.MaTruong = NganhTruong.MaTruong LEFT JOIN DiemChuan ON NganhTruong.MaTruong = DiemChuan.MaTruong AND NganhTruong.MaNganh = DiemChuan.MaNganh";

        private string S1
        {
            get { return Request["s1"] ?? SessionUtils.Best ?? "R"; }
        }

        private string S2
        {
            get { return Request["s2"] ?? SessionUtils.Second ?? "I"; }
        }

        private string Score
        {
            get { return Request["d"]; }
        }

        private string Excel
        {
            get { return Request["excel"] ?? string.Empty; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (SessionUtils.FullName == null)
            {
                Response.Redirect("/Index.aspx");
            }

            if (!IsPostBack)
            {
                this.ddlSoThich1.Items.Add("R");
                this.ddlSoThich1.Items.Add("C");
                this.ddlSoThich1.Items.Add("E");
                this.ddlSoThich1.Items.Add("I");
                this.ddlSoThich1.Items.Add("A");
                this.ddlSoThich1.Items.Add("S");

                this.ddlSoThich1.SelectedValue = S1;

                this.ddlSoThich2.Items.Add("R");
                this.ddlSoThich2.Items.Add("C");
                this.ddlSoThich2.Items.Add("E");
                this.ddlSoThich2.Items.Add("I");
                this.ddlSoThich2.Items.Add("A");
                this.ddlSoThich2.Items.Add("S");

                this.ddlSoThich2.SelectedValue = S2;

                var selectQuery = string.Empty;
                var condition = new List<string>();
                if (S1.Length < 2) condition.Add("(SoThich1 = '" + S1 + "' OR SoThich1 = '" + S2 + "')");
                if (S2.Length < 2) condition.Add("(SoThich2 = '" + S1 + "' OR SoThich2 = '" + S2 + "')");
                if (!String.IsNullOrEmpty(Score))
                {
                    decimal d;
                    if (Decimal.TryParse(Score.Trim(), out d))
                    {
                        condition.Add("(Diem >= " + (d - 3) + " AND Diem <= " + (d + 3) + ")");
                    }

                    this.txtDiemChuan.Text = Score;
                }
                if (condition.Count > 0)
                {
                    var cond = new string[condition.Count];
                    condition.CopyTo(cond, 0);

                    selectQuery = Query + " WHERE " + String.Join(" AND ", cond);
                }

                using (var helper = new TracNghiemChonTruong.Class.SqlHelper())
                {
                    this.dtgDiemChuan.DataSource = helper.ExecuteDataTable(selectQuery);
                    this.dtgDiemChuan.DataBind();

                    if (Excel == "1")
                    {
                        ExcelHelper.ExportExcel(this.dtgDiemChuan.DataSource,
                                                string.Format("diemchuan{0:yyyyMMdd}", DateTime.Today),
                                                "TRẮC NGHIỆM SỞ THÍCH VỀ ĐỊNH HƯỚNG NGHỀ NGHIỆP",
                                                new string[] { "Sở thích 1:", S1, "Điểm chuẩn:", Score },
                                                new string[] { "Sở thích 2:", S2, "Họ tên:", SessionUtils.FullName });
                    }
                }
            }
        }

        protected void RaiseBackEvent(object sender, EventArgs e)
        {
            Response.Redirect("/Default.aspx");
        }

        protected void RaiseSearchEvent(object sender, EventArgs e)
        {
            Response.Redirect("/SearchPage.aspx?s1=" + this.ddlSoThich1.SelectedValue + "&s2=" + this.ddlSoThich2.SelectedValue + "&d=" + this.txtDiemChuan.Text);
        }

        protected void RaiseExcelEvent(object sender, EventArgs e)
        {
            Response.Redirect("/SearchPage.aspx?excel=1&s1=" + this.ddlSoThich1.SelectedValue + "&s2=" + this.ddlSoThich2.SelectedValue + "&d=" + this.txtDiemChuan.Text);
        }
    }
}